/**
 * 收费服务实现类
 */
package com.p2p.front.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.p2p.base.constents.InnerAccountConsts;
import com.p2p.base.model.Money;
import com.p2p.front.component.MemberSetting;
import com.p2p.front.dal.model.ChargeLog;
import com.p2p.front.dal.model.MemberPoint;
import com.p2p.front.enums.ChargeLogStatus;
import com.p2p.front.enums.ChargeType;
import com.p2p.front.result.ChargeResult;
import com.p2p.front.service.ChargeLogService;
import com.p2p.front.service.ChargeService;
import com.p2p.front.service.MemberPointService;
import com.p2p.trans.client.enums.TransCodeEnum;
import com.p2p.trans.client.facade.AccountTransFacade;
import com.p2p.trans.client.model.TransInfo;
import com.p2p.trans.client.result.AccountTransResult;
import com.p2p.trans.client.utils.AccountUtil;

/**
 * @author ruiqi
 *
 */
@Service("chargeService")
public class ChargeServiceImpl implements ChargeService{

	protected final Logger logger = LoggerFactory.getLogger(ChargeServiceImpl.class);
	
	@Resource
	private AccountTransFacade remoteAccountTransFacade;
	
	@Resource
	private MemberSetting memberSetting;
	
	@Resource
	private MemberPointService memberPointService;
	
	@Resource
	private ChargeLogService chargeLogService;
	
	@Override
	public ChargeResult doChargeFromBalance(ChargeType chargeType, String accountNo,
			Long userId,Money fee, String memo) {
		//初始状态入库
		ChargeLog chargeLog = new ChargeLog();
		chargeLog.setUserAccountNo(accountNo);
		chargeLog.setUid(userId);
		chargeLog.setType(chargeType.name());
		chargeLog.setTransMemo(chargeType.getDefaultMemo());
		chargeLog.setTransAmount(fee);
		chargeLog.setStatus(ChargeLogStatus.INITIAL.getCode());
		chargeLogService.add(chargeLog);
		TransInfo transInfo = new TransInfo();
		
		transInfo.setTransCode(TransCodeEnum.TXCODE_CHARGE);
		transInfo.setTransAccountIn(InnerAccountConsts.innerChargeAccountNo);
		transInfo.setTransAccountOut(accountNo);
		transInfo.setTransAmount(fee);
		transInfo.setTransOutOrderNo(chargeLog.getId()+"");
		if(StringUtils.isNotBlank(memo)){
			transInfo.setTransMemo(memo);
		}else{
			transInfo.setTransMemo(chargeType.getDefaultMemo());
		}
		
		AccountTransResult accountTransResult = remoteAccountTransFacade.transfer(transInfo);
		ChargeResult chargeResult = new ChargeResult();
		if(!accountTransResult.isSuccess()){
			logger.error("do charge fall type="+chargeType.name()+",accountNo="+accountNo);
			//更新收费记录状态
			chargeLogService.updateFailed(chargeLog);
			chargeResult.setResultCode(accountTransResult.getResultCode());
			return chargeResult;
		}
		chargeLogService.updateSucceed(chargeLog);
		chargeResult.setSuccess(true);
		return chargeResult;
	}

	//TODO 做一个异步的扣费接口
	
	@Override
	public ChargeResult doChargeForInterest(String userId, Money interest) {
		Money fee = buildInterestManagementFee(userId, interest);
		String accountNo = AccountUtil.getInvestorsAccountNoByUserId(userId);
		return this.doChargeFromBalance(ChargeType.INTEREST_MANAGEMENT_CHARGE, accountNo,Long.parseLong(userId), fee, ChargeType.INTEREST_MANAGEMENT_CHARGE.getDefaultMemo());
	}

	
	public Money buildInterestManagementFee(String userId, Money interest) {
		MemberPoint memberPoint = memberPointService.getUserMemberPoint(userId);
		Double percentRate = this.memberSetting.getMemberLevelChargeRate(memberPoint.getLevel());
		Long feeCent = new Double(interest.getCent()*percentRate/100).longValue();
		Money fee = new Money(feeCent);
		return fee;
	}

	
}
